package com.uhealin.mr;

import java.io.IOException;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.text.MessageFormat;

public class JMap extends Mapper<LongWritable, Text, Text, Text> {

	public static final String SP = "\t";
	
	public static final JsonParser JP = new JsonParser();

	@Override
	protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context)
			throws IOException, InterruptedException {
		// TODO 자동 생성된 메소드 스텁
		 String line=value.toString();
		 String[] lines=line.split(SP);
		 if(lines.length!=2)return;
		 
		 String clazz=lines[0],json=lines[1];
		 JsonArray jarr=JP.parse(json).getAsJsonArray();
		 int jsize=jarr.size();
		 for(int i=0;i<jsize;i++){
			JsonObject jobj=jarr.get(i).getAsJsonObject();
			String name=jobj.get("name").getAsString()
					,age=jobj.get("age").getAsString()
					,math=jobj.get("math").getAsString();
			String mf=MessageFormat.format("{1}{0}{2}{0}{3}",SP,name,age,math);
			context.write(new Text(clazz), new Text(mf));
		 }
	}

}
